package com.pro_one.controller;

import com.pro_one.constant.MessageConstant;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.entity.QueryPageBeanQueryString;
import com.pro_one.entity.Result;
import com.pro_one.service.OrderService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderService orderService;
    @RequestMapping("/findByCondition")
        public PageResult findByCondition(@RequestBody QueryPageBeanQueryString queryPageBean){
        PageResult pageResult = orderService.PageQuery(queryPageBean);
        return pageResult;
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Map<String,Object> map,Integer[] setmealIds){
        try{
            List<Integer> lsetmealIds = new ArrayList<>();
            for (Integer setmealId : setmealIds) {
                lsetmealIds.add(setmealId);
            }
            Result result = orderService.add(map,lsetmealIds);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDER_FAIL);
        }
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Map map = orderService.findByOrderId(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
    @RequestMapping("/findSetmealIdsByOrderId")
    public Result findSetmealIdsByOrderId(Integer id){
        try{
            List<Integer> list = orderService.findSetmealIdsByOrderId(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Map map,Integer[] setmealIds){
        try {
            List<Integer> lsetmealIds = new ArrayList<>();
            for (Integer setmealId : setmealIds) {
                lsetmealIds.add(setmealId);
            }
            orderService.edit(map,lsetmealIds);
            return new Result(true,MessageConstant.EDIT_ORDER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ORDER_FAIL);
        }
    }
    @RequestMapping("/changeOrderStatus")
    public Result changeOrderStatus(Integer id){
        try {
            orderService.changeOrderStatus(id);
            return new Result(true,MessageConstant.CHANGEORDERSTATUS_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.CHANGEORDERSTATUS_FAIL);
        }
    }
    @RequestMapping("/forbidden")
    public Result forbidden(Integer id){
        try{
            orderService.forbidden(id);
            return new Result(true,MessageConstant.FORBIDDEN_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.FORBIDDEN_FAIL);
        }
    }
}
